package org.sauceggplant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 加密界面
 *
 * @author jacob
 * @version 0.1
 */
public class EPanel extends JPanel {

    private JFileChooser eFileChooser = new JFileChooser();

    private JProgressBar eProgressBar = new JProgressBar();

    /**
     * 加密
     */
    private Encrypt encrypt;

    public EPanel() {
        this.setLayout(new BorderLayout());

        eFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        eFileChooser.setMultiSelectionEnabled(false);
        eFileChooser.setApproveButtonText("加密");

        eProgressBar.setMinimum(0);
        eProgressBar.setMaximum(100);
        eProgressBar.setStringPainted(true);
        eProgressBar.setOrientation(SwingConstants.HORIZONTAL);

        encrypt = new Encrypt(eProgressBar);
        eFileChooser.addActionListener(new EncryptListener(eFileChooser, encrypt));

        this.add(eFileChooser, BorderLayout.CENTER);
        this.add(eProgressBar, BorderLayout.SOUTH);
    }

    public JFileChooser geteFileChooser() {
        return eFileChooser;
    }
}

class EncryptListener implements ActionListener {

    /**
     * 文件选择器
     */
    private JFileChooser fileChooser;

    private Encrypt encrypt;

    public EncryptListener(JFileChooser fileChooser, Encrypt encrypt) {
        this.fileChooser = fileChooser;
        this.encrypt = encrypt;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("ApproveSelection".equals(e.getActionCommand())) {
            File file = fileChooser.getSelectedFile();
            if (file != null) {
                encrypt.encrypt(file);
                fileChooser.updateUI();
            }
        }
    }
}